package com.dmwork.vod.controller.front.me;

import com.aliyuncs.exceptions.ClientException;
import com.dmwork.common.base.result.ExceptionUtils;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.result.ResultCodeEnum;
import com.dmwork.service.base.exception.DmworkException;
import com.dmwork.service.base.model.VideoListVo;
import com.dmwork.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
@Api(description = "视频信息处理")
@RequestMapping("/front/dm_work/vod/me")
@RestController
public class VodMeController {

    @Autowired
    private VodService vodService;

    @ApiOperation("视频上传")
    @PostMapping("upload")
    public R upload(@ApiParam(name = "file", value = "文件", required = true) @RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String videoSourceId = vodService.uploadVideo(inputStream, originalFilename);
            return R.ok().message("视频上传成功").data("videoSourceId", videoSourceId);
        } catch (IOException e) {
            log.info(ExceptionUtils.getMessage(e));
            throw new DmworkException(ResultCodeEnum.VIDEO_UPLOAD_TOMCAT_ERROR);
        }

    }

    @ApiOperation("根据id删除视频")
    @DeleteMapping("remove/{videoSourceId}")
    public R removeVideo(@ApiParam(value = "视频资源id") @PathVariable("videoSourceId") String videoSourceId) {
        try {
            vodService.removeVideo(videoSourceId);
            return R.ok().message("视频删除成功");
        } catch (ClientException e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new DmworkException(ResultCodeEnum.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }

    @ApiOperation("根据视频id列表删除所有视频")
    @DeleteMapping("remove")
    public R removeVideoByIdList(
            @ApiParam(value = "阿里云视频id列表", required = true)
            @RequestBody VideoListVo videoListVo) {
        try {
            vodService.removeVideoByVideoIdList(videoListVo.getVideoIdList());
            return R.ok().message("视频删除成功");
        } catch (ClientException e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new DmworkException(ResultCodeEnum.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }
}
